<template>
  <div class="faq-manage">
    <div class="page-header">
      <h1>FAQ管理</h1>
      <button @click="showAddDialog = true" class="add-btn">
        <span class="btn-icon">➕</span>
        添加FAQ
      </button>
    </div>

    <div class="search-bar">
      <input 
        v-model="searchKeyword" 
        @input="searchFAQs"
        placeholder="搜索问题..." 
        class="search-input"
      >
    </div>

    <div class="faqs-table">
      <table>
        <thead>
          <tr>
            <th>ID</th>
            <th>问题</th>
            <th>答案</th>
            <th>分类</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="faq in filteredFAQs" :key="faq.id">
            <td>{{ faq.id }}</td>
            <td class="question-cell">{{ faq.question }}</td>
            <td class="answer-cell">{{ faq.answer.substring(0, 60) }}...</td>
            <td class="category-cell">
              <span class="category-tag">{{ faq.category }}</span>
            </td>
            <td class="actions-cell">
              <button @click="editFAQ(faq)" class="edit-btn">编辑</button>
              <button @click="deleteFAQ(faq.id)" class="delete-btn">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 添加/编辑对话框 -->
    <div v-if="showAddDialog || showEditDialog" class="dialog-overlay" @click="closeDialog">
      <div class="dialog" @click.stop>
        <div class="dialog-header">
          <h2>{{ showAddDialog ? '添加FAQ' : '编辑FAQ' }}</h2>
          <button @click="closeDialog" class="close-btn">✕</button>
        </div>
        <div class="dialog-body">
          <form @submit.prevent="saveFAQ">
            <div class="form-group">
              <label>问题：</label>
              <input v-model="currentFAQ.question" type="text" required>
            </div>
            <div class="form-group">
              <label>答案：</label>
              <textarea v-model="currentFAQ.answer" rows="6" required></textarea>
            </div>
            <div class="form-group">
              <label>分类：</label>
              <select v-model="currentFAQ.category" required>
                <option value="">请选择分类</option>
                <option value="基本信息">基本信息</option>
                <option value="出行交通">出行交通</option>
                <option value="住宿问题">住宿问题</option>
                <option value="景点门票">景点门票</option>
                <option value="其他">其他</option>
              </select>
            </div>
            <div class="form-actions">
              <button type="button" @click="closeDialog" class="cancel-btn">取消</button>
              <button type="submit" class="save-btn">保存</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

interface FAQ {
  id?: number
  question: string
  answer: string
  category: string
}

const faqs = ref<FAQ[]>([])
const filteredFAQs = ref<FAQ[]>([])
const searchKeyword = ref('')
const showAddDialog = ref(false)
const showEditDialog = ref(false)
const currentFAQ = ref<FAQ>({
  question: '',
  answer: '',
  category: ''
})

const loadFAQs = async () => {
  try {
    const response = await fetch('http://localhost:8080/api/faqs')
    if (response.ok) {
      faqs.value = await response.json()
      filteredFAQs.value = faqs.value
    }
  } catch (error) {
    console.error('加载FAQ数据失败:', error)
    alert('加载FAQ数据失败')
  }
}

const searchFAQs = () => {
  if (!searchKeyword.value.trim()) {
    filteredFAQs.value = faqs.value
  } else {
    filteredFAQs.value = faqs.value.filter(faq =>
      faq.question.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      faq.answer.toLowerCase().includes(searchKeyword.value.toLowerCase())
    )
  }
}

const editFAQ = (faq: FAQ) => {
  currentFAQ.value = { ...faq }
  showEditDialog.value = true
}

const deleteFAQ = async (id: number | undefined) => {
  if (id === undefined) {
    alert('无法删除，FAQ ID未知。');
    return;
  }
  if (!confirm('确定要删除这个FAQ吗？')) return

  try {
    const response = await fetch(`http://localhost:8080/api/faqs/${id}`, {
      method: 'DELETE'
    })
    
    if (response.ok) {
      alert('删除成功')
      loadFAQs()
    } else {
      alert('删除失败')
    }
  } catch (error) {
    console.error('删除FAQ失败:', error)
    alert('删除FAQ失败')
  }
}

const saveFAQ = async () => {
  try {
    const url = showAddDialog.value 
      ? 'http://localhost:8080/api/faqs'
      : `http://localhost:8080/api/faqs/${currentFAQ.value.id}`
    
    const method = showAddDialog.value ? 'POST' : 'PUT'
    
    const response = await fetch(url, {
      method,
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(currentFAQ.value)
    })
    
    if (response.ok) {
      alert(showAddDialog.value ? '添加成功' : '更新成功')
      closeDialog()
      loadFAQs()
    } else {
      alert(showAddDialog.value ? '添加失败' : '更新失败')
    }
  } catch (error) {
    console.error('保存FAQ失败:', error)
    alert('保存FAQ失败')
  }
}

const closeDialog = () => {
  showAddDialog.value = false
  showEditDialog.value = false
  currentFAQ.value = {
    question: '',
    answer: '',
    category: ''
  }
}

onMounted(() => {
  loadFAQs()
})
</script>

<style scoped>
.faq-manage {
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.page-header h1 {
  color: #2c3e50;
  margin: 0;
}

.add-btn {
  display: flex;
  align-items: center;
  padding: 10px 20px;
  background: #27ae60;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.3s;
}

.add-btn:hover {
  background: #229954;
}

.btn-icon {
  margin-right: 8px;
}

.search-bar {
  margin-bottom: 20px;
}

.search-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.faqs-table {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  overflow: hidden;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

th {
  background: #f8f9fa;
  font-weight: 600;
  color: #2c3e50;
}

.question-cell {
  font-weight: 500;
  color: #2c3e50;
  max-width: 250px;
}

.answer-cell {
  color: #666;
  max-width: 300px;
}

.category-cell .category-tag {
  background: #3498db;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.actions-cell {
  white-space: nowrap;
}

.edit-btn, .delete-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  margin-right: 8px;
}

.edit-btn {
  background: #3498db;
  color: white;
}

.edit-btn:hover {
  background: #2980b9;
}

.delete-btn {
  background: #e74c3c;
  color: white;
}

.delete-btn:hover {
  background: #c0392b;
}

.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog {
  background: white;
  border-radius: 10px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.dialog-header h2 {
  margin: 0;
  color: #2c3e50;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
}

.close-btn:hover {
  color: #666;
}

.dialog-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #2c3e50;
}

.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.form-group textarea {
  resize: vertical;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 30px;
}

.cancel-btn, .save-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
}

.cancel-btn {
  background: #95a5a6;
  color: white;
}

.cancel-btn:hover {
  background: #7f8c8d;
}

.save-btn {
  background: #27ae60;
  color: white;
}

.save-btn:hover {
  background: #229954;
}
</style> 